package nix.anim.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import nix.anim.common.pojo.NixResult;
import nix.anim.mapper.MenuMapper;
import nix.anim.mapper.RightMapper;
import nix.anim.pojo.Menu;
import nix.anim.pojo.Right;
import nix.anim.pojo.RightExample;
import nix.anim.service.RightService;

/**
 * 权限管理Service
 */
@Service
public class RightServiceImpl implements RightService {

	@Autowired
	private RightMapper rightMapper;
	
	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public NixResult deleteAll() {
		// 删除所有
		rightMapper.deleteByExample(new RightExample());
		return NixResult.ok();
	}

	@Override
	public NixResult addRight(Right right) {
		// 判断菜单是否存在
		Menu menu = menuMapper.selectByPrimaryKey(right.getMenuId());
		if (menu == null) {
			return NixResult.build(400, "关联的菜单不存在！");
		}
		// 添加
		rightMapper.insert(right);
		return NixResult.ok();
	}

	@Override
	public NixResult getRightList() {
		// 查询
		List<Right> list = rightMapper.selectByExample(new RightExample());
		return NixResult.ok(list);
	}
	
}
